package com.n5x.model.shopping;

import com.n5x.model.AbstractModel;
import com.n5x.model.user.UserDisCount;
import com.n5x.model.user.UserInfo;
import org.springframework.data.annotation.Transient;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ShoppingOrder extends AbstractModel {


    public ShoppingOrder() {
    }

    public ShoppingOrder(String fkUser, ShoppingOrderType shoppingOrderType) {
        this.fkUser = fkUser;
        this.type = shoppingOrderType.name();
        this.currentState = ShoppingState.未付款.getState();
//        for (ShoppingOrderDetails d : orderDetails) {
//            this.totalMoney = totalMoney.add(d.getPrice().multiply(new BigDecimal(d.getQty())));
//            if (d.getPreferentialPrice() != null && d.getPreferentialPrice().compareTo(BigDecimal.ZERO) != 0) {
//                this.actualMoney = this.actualMoney.add(d.getPreferentialPrice().multiply(new BigDecimal(d.getQty())));
//                this.discountMoney = this.discountMoney.add(d.getPreferentialPrice().multiply(new BigDecimal(d.getQty())));
//            } else {
//                this.actualMoney = this.actualMoney.add(d.getPrice().multiply(new BigDecimal(d.getQty())));
//                this.discountMoney = this.discountMoney.add(d.getPrice().multiply(new BigDecimal(d.getQty())));
//            }
//        }
    }

    @Transient
    public ShoppingOrder settingUserDiscount(List<UserDisCount> userDisCountList) {
        for (UserDisCount userDisCount : userDisCountList) {
            this.discountMoney = this.discountMoney.add(userDisCount.getDiscountPrice());
            this.actualMoney = this.totalMoney.subtract(userDisCount.getDiscountPrice());
        }
        return this;
    }

    public enum ShoppingOrderType {
        商品订单, 礼品订单;

        public static ShoppingOrderType getShoppingOrderType(Integer ordinal) {
            if (ordinal == null) return null;
            int size = ShoppingOrderType.values().length;
            if (ordinal < 0 || ordinal > size - 1) return null;
            return ShoppingOrderType.values()[ordinal];
        }
    }

    public enum ShoppingState {
        未付款("0"), 已付款("1"), 接单("2"), 派单("3"), 送货("4"), 送达("5"), 完成("6"), 订单退回("7"),取消订单("8");

        private String state;

        public String getState() {
            return state;
        }

        ShoppingState(String state) {
            this.state = state;
        }

        @Override
        public String toString() {
            return this.state;
        }
    }

    public enum TransactionMethod {
        现金("0"), 微信("1"), 支付宝("2"), 余额("3"), 优惠券("4");

        private String method;

        public String getMethod() {
            return method;
        }

        TransactionMethod(String method) {
            this.method = method;
        }
    }

    /**
     * 关联用户
     */
    private String fkUser;

    /**
     * 订单类型
     *
     * @See ShoppingOrderType
     */
    private String type;

    /**
     * 取货码
     */
    private String receiptCode;

    /**
     * 合计金额
     */
    private BigDecimal totalMoney = new BigDecimal(0);

    /**
     * 实际金额
     */
    private BigDecimal actualMoney = new BigDecimal(0);

    /**
     * 优惠金额
     */
    private BigDecimal discountMoney = new BigDecimal(0);

    /**
     * 关联快递柜
     */
    private String fkErpressBox;

    /**
     * 快递地址
     */
    private String erpressBoxAddress;

    /**
     * 备注
     */
    private String remark;

    /**
     * 客户预订时间
     */
    private String booksingTime;

    /**
     * 支付时间
     */
    private Date payTime;

    /**
     * 预计到达时间
     */
    private Date arrivalTime;

    /**
     * 订单完成时间
     */
    private Date finishTime;

    /**
     * 当前状态
     */
    private String currentState;

    /**
     * 交易号码
     */
    private String transactionNumber;

    /**
     * 交易方式
     */
    private String transactionMethod;

    /**
     * 是否余额支付
     */
    private String isBalance;

    /**
     * 是否发货
     */
    private String isSendToHome;

    /**
     * 送货员
     */
    private String deliver;

    @Transient
    private UserInfo userInfo;

    @Transient
    private List<ShoppingOrderDetails> details = new ArrayList<ShoppingOrderDetails>();

    public String getFkUser() {
        return fkUser;
    }

    public void setFkUser(String fkUser) {
        this.fkUser = fkUser;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getReceiptCode() {
        return receiptCode;
    }

    public void setReceiptCode(String receiptCode) {
        this.receiptCode = receiptCode;
    }

    public BigDecimal getTotalMoney() {
        return totalMoney;
    }

    public void setTotalMoney(BigDecimal totalMoney) {
        this.totalMoney = totalMoney;
    }

    public BigDecimal getActualMoney() {
        return actualMoney;
    }

    public void setActualMoney(BigDecimal actualMoney) {
        this.actualMoney = actualMoney;
    }

    public BigDecimal getDiscountMoney() {
        return discountMoney;
    }

    public void setDiscountMoney(BigDecimal discountMoney) {
        this.discountMoney = discountMoney;
    }

    public String getFkErpressBox() {
        return fkErpressBox;
    }

    public void setFkErpressBox(String fkErpressBox) {
        this.fkErpressBox = fkErpressBox;
    }

    public String getErpressBoxAddress() {
        return erpressBoxAddress;
    }

    public void setErpressBoxAddress(String erpressBoxAddress) {
        this.erpressBoxAddress = erpressBoxAddress;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getBooksingTime() {
        return booksingTime;
    }

    public void setBooksingTime(String booksingTime) {
        this.booksingTime = booksingTime;
    }

    public Date getPayTime() {
        return payTime;
    }

    public void setPayTime(Date payTime) {
        this.payTime = payTime;
    }

    public Date getArrivalTime() {
        return arrivalTime;
    }

    public void setArrivalTime(Date arrivalTime) {
        this.arrivalTime = arrivalTime;
    }

    public Date getFinishTime() {
        return finishTime;
    }

    public void setFinishTime(Date finishTime) {
        this.finishTime = finishTime;
    }

    public String getCurrentState() {
        return currentState;
    }

    public void setCurrentState(String currentState) {
        this.currentState = currentState;
    }

    public String getTransactionNumber() {
        return transactionNumber;
    }

    public void setTransactionNumber(String transactionNumber) {
        this.transactionNumber = transactionNumber;
    }

    public String getTransactionMethod() {
        return transactionMethod;
    }

    public void setTransactionMethod(String transactionMethod) {
        this.transactionMethod = transactionMethod;
    }

    public String getIsBalance() {
        return isBalance;
    }

    public void setIsBalance(String isBalance) {
        this.isBalance = isBalance;
    }

    public String getIsSendToHome() {
        return isSendToHome;
    }

    public void setIsSendToHome(String isSendToHome) {
        this.isSendToHome = isSendToHome;
    }

    public String getDeliver() {
        return deliver;
    }

    public void setDeliver(String deliver) {
        this.deliver = deliver;
    }

    public List<ShoppingOrderDetails> getDetails() {
        return details;
    }

    public void setDetails(List<ShoppingOrderDetails> details) {
        this.details = details;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }
}